/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rhythm.commons.csv;

import static com.rhythm.base.Preconditions.*;
import com.rhythm.commons.collections.Maps;
import java.util.Map;

/**
 *
 * @author mlee
 */
public class CSVRow {

    private final Object DEFAULT_VALUE = null;
    private final Map<String, Object> values = Maps.newHashMap();

    /**
     * Constructs a new instance of {@code CSVRow}
     * @param columns The column names
     */
    public CSVRow(final String[] columns) {
        buildRow(columns);
    }

    /**
     * Constructs a new instance of {@code CSVRow} wit the given column names and row values
     * @param columns column names
     * @param values row values
     * @throws IllegalArgumentException if the number of columns does not equal the number of values
     */
    public CSVRow(final String[] columns, final Object[] values) {
        buildRow(columns, values);
    }

    private void buildRow(final String[] columns) {
        for (String col : columns) {
            values.put(col, DEFAULT_VALUE);
        }
    }

    private void buildRow(final String[] columns, final Object[] values) {
        assertArgument(columns.length == values.length, "Columns don't match rows");

        for (int i = 0; i < columns.length; i++) {
            this.values.put(columns[i], values[i]);
        }
    }

    /**
     * Returns the number of columns in the current instance.
     * @return number
     */
    public int getColCount(){
        return values.size();
    }

    /**
     * Returns the row value given a column name
     * @param <T>
     * @param column
     * @return
     */
    public <T> T get(String column){
        return (T) this.values.get(column);
    }

    @Override
    public String toString() {
        return Maps.toString(values);
    }



}
